#include "cvisionmatterelementset.h"

CVisionMatterElementSet::CVisionMatterElementSet()
{
    this->m_p_vse = NULL;
    this->m_width = this->m_height = 0;
}

CVisionMatterElementSet::~CVisionMatterElementSet()
{
    if (this->m_p_vse != NULL){
        delete this->m_p_vse;
        this->m_p_vse = NULL;
    }
    this->m_width = this->m_height = 0;
}

/********************************************
 *
 * 将视觉物质元集合符号化，
 * 机器视觉接收器从这里开始接受视觉信息
 * 这里，BMP数据相当于外部光线刺激
 *
 ********************************************/
int CVisionMatterElementSet::to_be_symbolic(XBitmap *pbmp)
{
    if (pbmp == NULL ) return -1; // 位图指针为空
    if (pbmp->get_buf() == NULL) return -1; // 位图包含数据无效
    if ((pbmp->get_nt() < 1 ) || (pbmp->get_mt() < 1 )) {
        return -2; //位图数据量为0
    }

    this->m_p_vse = new
            SVisionSymbolElement[pbmp->get_nt() * pbmp->get_nt()];

    if (this->m_p_vse == NULL) return -3; // 分配内存失败

    this->m_width = pbmp->get_nt();
    this->m_height = pbmp->get_mt();

    char * buf_bmp = pbmp->get_buf();
    unsigned long i = 0;

    for(unsigned long m = 0; m < m_height; m++) {
        for(unsigned long n = 0; n < m_width; n++){
            unsigned char r = buf_bmp[m*m_width*3 + n*3 + 0];
            unsigned char g = buf_bmp[m*m_width*3 + n*3 + 1];
            unsigned char b = buf_bmp[m*m_width*3 + n*3 + 2];
            m_p_vse[i].R = r;
            m_p_vse[i].G = g;
            m_p_vse[i].B = b;
            m_p_vse[i].Y = ((299 * r) + (587 * g) + (114 * b)) / 1000;

            i++;
        }
    }

    return 0;
}

PSVisionSymbolElement CVisionMatterElementSet::get_vmes_data()
{
    return this->m_p_vse;
}

int CVisionMatterElementSet::get_width()
{
    return this->m_width;
}

int CVisionMatterElementSet::get_height()
{
    return this->m_height;
}
